using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Headspring.Training.Core.Model;
using Headspring.Training.Core.Services;
using Headspring.Training.UI.Controllers;
using MvcContrib;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;

namespace Headspring.Training.UnitTests.UI.Controllers
{
	[TestFixture]
	public class SearchControllerTester
	{
//		[Test, Explicit]
//		public void ShouldRequireLoggedInEmployee()
//		{
//			var filters = (ActionFilterAttribute[]) typeof (SearchController)
//			                                        	.GetCustomAttributes(
//			                                        	typeof (ActionFilterAttribute),
//			                                        	true);
//
//			Assert.That(filters.Length, Is.EqualTo(1));
//			Assert.That(filters[0].GetType(),
//			            Is.EqualTo(typeof (AuthenticationFilterAttribute)));
//		}

		public class WorkOrderRepositoryStub : IWorkOrderRepository
		{
			private readonly int _numberOfWorkOrdersToReturn;
			public WorkOrderSearchSpecification _specification;

			public WorkOrderRepositoryStub(int numberOfWorkOrdersToReturn)
			{
				_numberOfWorkOrdersToReturn = numberOfWorkOrdersToReturn;
			}

			public WorkOrder[] FindWorkOrders(
				WorkOrderSearchSpecification specification)
			{
				_specification = specification;

				var list = new List<WorkOrder>();
				for (int i = 0; i < _numberOfWorkOrdersToReturn; i++)
				{
					list.Add(new WorkOrder());
				}

				return list.ToArray();
			}

			public void Save(WorkOrder workOrder)
			{
				throw new NotImplementedException();
			}

			public WorkOrder GetWorkOrder(string number)
			{
				throw new NotImplementedException();
			}
		}

		public class EmployeeRepositoryStub : IEmployeeRepository
		{
			private readonly Employee[] _allEmployees;

			public EmployeeRepositoryStub(params Employee[] employees)
			{
				_allEmployees = employees;
			}

			public Employee GetByUserName(string userName)
			{
				return _allEmployees.Single(employee => employee.UserName == userName);
			}

			public Employee[] GetAllEmployeesSorted()
			{
				return _allEmployees;
			}
		}

		[Test]
		public void ShouldListWorkOrderBasedOnSelection()
		{
			var ned = new Employee("nflanders", "Ned", "Flander", "ned@flanders.com");
			var homer = new Employee("hsimpson", "Homer", "Simpson",
			                         "homer@simpsons.com");

			var employeeRepository = new EmployeeRepositoryStub(ned, homer);
			var workOrderRepository = new WorkOrderRepositoryStub(987);

			var controller = new SearchController(workOrderRepository, employeeRepository);

			var result = (ViewResult) controller.List("hsimpson", "nflanders",
			                                          WorkOrderStatus.Assigned.
			                                          	Key);

			Assert.That(result.ViewName, Is.EqualTo("index"));
			Assert.That(result.ViewData.Contains<WorkOrder[]>());

			var workOrders = result.ViewData.Get<WorkOrder[]>();
			Assert.That(workOrders.Length, Is.EqualTo(987));

			WorkOrderSearchSpecification spec = workOrderRepository._specification;
			Assert.That(spec.Creator, Is.EqualTo(ned));
			Assert.That(spec.Assignee, Is.EqualTo(homer));
			Assert.That(spec.Status, Is.EqualTo(WorkOrderStatus.Assigned));
		}
	}
}